<%@page import="com.google.appengine.api.datastore.EntityNotFoundException"%>
<%@page import="java.util.ArrayList" %>
<%@page import="javax.servlet.http.HttpSession" %>
<%@page import="nl.project.data.Student" %>
<%@page import="nl.project.data.Parent" %>
<%@page import="nl.project.data.Employee" %>
<%@page import="nl.project.data.controller.MembersDAO" %>

<html>
	<head>
		<title>Selectie leerling</title>
		<link rel="stylesheet" type="text/css" href="/include/style/style.css" />
	</head>
	<body>
		<jsp:include page="include/header.jsp" />
		<h4>Selecteer de ouder waar u een afspraak voor wilt maken: </h4>
		<%
			boolean val = false;
			try{
				try{
					Employee empl = (Employee) session.getAttribute("user");
					val = true;
					throw new RuntimeException();
				} catch(Exception eq){
					if(val){
						throw new RuntimeException();
					}
				}
				out.print("<form action='registration' method='POST'>");
				Parent p = (Parent) session.getAttribute("user");
			
				for (int i = 0; i < p.getStudents().size(); i++) {
					try {
						Student s = new Student(p.getStudents().get(i).toString());
						out.println("<input type=\"radio\" name=\"student_id\" "+
					            	"       value=\""+s.getStudentId()+"\">Naam: " + s.getName() + "</input><br>");
					} catch (EntityNotFoundException e) {
						out.println("Student niet gevonden...<br>");
					}
				}
				%>
				<input type="submit" value="Selecteer kind">
				</form>
				<%
			} catch(Exception e) {
				ArrayList<Parent> parentList=null;
				try{
					parentList = MembersDAO.getAllParents();
				%>
				<form action="student_select.jsp" method="POST">
				<%
				out.print("<select name='parentMenu'>");
				int pid = 0;
				try {
					pid = Integer.parseInt(request.getParameter("parentMenu").toString());
				} catch(Exception exep){}
				for (Parent p : parentList) {
					out.print("<option ");
					if(p.getParentId() == pid){
						out.println(" selected='selected' ");
					}
					out.println("value='"+ p.getParentId() +"'>"+p.getName()+"</option>");
				}
				
				out.println("</select>");}
				
				catch(Exception ex){ex.printStackTrace();}
				
				out.print("<input type='submit'");
				
				out.print("value='Selecteer ouder'>");
				
				%>
				<input type='hidden' name='disabled' value='yes'>
				</form>
				<form action="registration" method="POST">
				<%
				out.print("<select name='student_id'>");
				if(request.getParameter("parentMenu") != null){
					
					int parentID = Integer.parseInt(request.getParameter("parentMenu").toString());
					request.getSession().setAttribute("parentId", parentID);
					ArrayList<Student> students = new ArrayList<Student>();
					
					for (Parent p : parentList) {
						if(p.getParentId() == parentID){
							
							ArrayList<Integer> studentIDs = new ArrayList<Integer>();
							int y = 0;
							for(Object o : p.getStudents()){
								y = Integer.parseInt(o.toString());
								studentIDs.add(y);
							}
							for(Integer id : studentIDs){
								Student s = new Student(id);
								students.add(s);
								
							}
							break;
						}
					}
					for(Student s : students){
						out.print("<option value='"+ s.getStudentId() +"'>"+s.getName()+"</option>");
					}
					System.out.println(request.getParameter("disabled"));
				}
				
				
				%>
					</select>
					<input type="submit" <%if(request.getParameter("disabled") == null) {
						out.print(" disabled='disabled' ");
					} %>value="Selecteer student">
					</form>
				<%
			
			}
		%>
	</body>
</html>